#
#	FILE:	 PlanetFallHighlands.py
#	AUTHOR:  Oleg Giwodiorow / Refar
#                lord_refar@yahoo.de
#       CONTRIB: Examples and Code Snipplets from original CivIV scripts
#	PURPOSE: Map Script for Civ4 Planetfall Mod. 
#	with some minor modifications by Maniac
#                            
#-----------------------------------------------------------------------------
# 
#-----------------------------------------------------------------------------
#
from CvPythonExtensions import *
import CvUtil
import CvMapGeneratorUtil
import random
import sys
from math import sqrt
from CvMapGeneratorUtil import MultilayeredFractal
from CvMapGeneratorUtil import FractalWorld
from CvMapGeneratorUtil import TerrainGenerator
from CvMapGeneratorUtil import FeatureGenerator
from CvMapGeneratorUtil import BonusBalancer

balancer = BonusBalancer()

#-------------------------------------------------------------------------
# Finetuning Constants
#
# Amount of Highlands.
# Range [1, 100] (I think)
# Higher number means LESS.
h_highlands = 60
# Lower grain will i.g. produce more coherent areas.
# Range [1, ->].
# Values below 4 are likey to produce just one big blobb...
h_grain = 4
# Amount of Peaks. Is derived from amount of highlands, hence as fraction
# Range [0, 1]
# 0 ~ no Peaks. 1 will probably cover all the map with them.
# Here more means more. Sorry. 
h_peaks = 0.25

#-------------------------------------------------------------------------
# 
#
def getDescription():
	return "TXT_KEY_MAP_SCRIPT_GLOBAL_HIGHLANDS_DESCR"

def isAdvancedMap():
	"This map should show up in simple mode"
	return 0

#-------------------------------------------------------------------------
# 
#
def getNumCustomMapOptions():
	return 3

#-------------------------------------------------------------------------
# 
#
def getCustomMapOptionName(argsList):
	[iOption] = argsList
	option_names = {
		0:	"TXT_KEY_MAP_SCRIPT_CONTINENTS_SIZE",
		1:	"TXT_KEY_MAP_SCRIPT_ISLANDS_SIZE",
		2:	"TXT_KEY_MAP_SCRIPT_ISLAND_OVERLAP"
		}
	translated_text = unicode(CyTranslator().getText(option_names[iOption], ()))
	return translated_text

#-------------------------------------------------------------------------
# 
#
def getNumCustomMapOptionValues(argsList):
	[iOption] = argsList
	option_values = {
		0:	3,
		1:	2,
		2:	2
		}
	return option_values[iOption]

#-------------------------------------------------------------------------
# 
#	
def getCustomMapOptionDescAt(argsList):
	[iOption, iSelection] = argsList
	selection_names = {
		0:	{
			0: "TXT_KEY_MAP_SCRIPT_MASSIVE_CONTINENTS",
			1: "TXT_KEY_MAP_SCRIPT_NORMAL_CONTINENTS",
			2: "TXT_KEY_MAP_SCRIPT_SNAKY_CONTINENTS"
			},
		1:	{
			0: "TXT_KEY_MAP_SCRIPT_ISLANDS",
			1: "TXT_KEY_MAP_SCRIPT_TINY_ISLANDS"
			},
		2:	{
			0: "TXT_KEY_MAP_SCRIPT_ISLAND_REGION_SEPARATE",
			1: "TXT_KEY_MAP_SCRIPT_ISLANDS_MIXED_IN"
			}
		}
	translated_text = unicode(CyTranslator().getText(selection_names[iOption][iSelection], ()))
	return translated_text

#-------------------------------------------------------------------------
# 
#
def getCustomMapOptionDefault(argsList):
	[iOption] = argsList
	option_defaults = {
		0:	1,
		1:	0,
		2:	1
		}
	return option_defaults[iOption]
	
def beforeGeneration():
	global xShiftRoll
	gc = CyGlobalContext()
	dice = gc.getGame().getMapRand()

	# Binary shift roll (for horizontal shifting if Island Region Separate).
	xShiftRoll = dice.get(2, "Region Shift, Horizontal - Big and Small PYTHON")
	print xShiftRoll
	
def minStartingDistanceModifier():
	return -25

#-----------------------------------------------------------------------------
# AUTHORS: Oleg Giwodiorow (Refar)
# Subclassing Multilayered fractal, in order to overwrite the
# generatePlotsInRegion() method, so thait it uses the Rift_Grain
# parameter now.
# The goal is being able to create regions usig the same settings, as
# the normal fractal map genetaror does.
# This to be able to make one layer covering all map, and being the same
# as the normal 'Fractal' map type, but with the possibility to add
# more layers for islands or specific features.
#-----------------------------------------------------------------------------
class PFHL_MultilayeredFractal(CvMapGeneratorUtil.MultilayeredFractal):
	#-------------------------------------------------------------------------
	# Overwriting to include Wrap Check - better results if one Region 
	# covers the entire map.
	#
	def shiftRegionPlots(self, iRegionWidth, iRegionHeight, iStrip=15):
		stripRadius = min(15, iStrip)
		stripRadius = max(3, iStrip)
		best_split_x = 0
		best_split_y = 0

		if self.map.isWrapX() :
			best_split_x = self.findBestRegionSplitX(iRegionWidth, iRegionHeight, stripRadius)
		if self.map.isWrapY() :
			best_split_y = self.findBestRegionSplitY(iRegionWidth, iRegionHeight, stripRadius)

		self.shiftRegionPlotsBy(best_split_x, best_split_y, iRegionWidth,	iRegionHeight)
	
	#-------------------------------------------------------------------------
	# Overwriting to change the parameters of the regional fractals.
	# Main Change - the RegionalFractal now can have a RiftFractal
	def generatePlotsInRegion(self, iWaterPercent, iRegionWidth, iRegionHeight, iRegionWestX, iRegionSouthY, 
	iRegionGrain, iRegionHillsGrain, iRegionPlotFlags, iRegionTerrainFlags, iRegionFracXExp = -1, iRegionFracYExp = -1, 
	bShift = True, iStrip = 15, rift_grain = -1, has_center_rift = False, invert_heights = False):
		# This is the code to generate each fractal.
		#
		# Init local variables
		water = iWaterPercent
		iWestX = iRegionWestX
		# Note: Do not pass bad regional dimensions so that iEastX > self.iW
		iSouthY = iRegionSouthY
		# Init the plot types array and the regional fractals
		self.plotTypes = [] # reinit the array for each pass
		self.plotTypes = [PlotTypes.PLOT_OCEAN] * (iRegionWidth*iRegionHeight)
		regionContinentsFrac = CyFractal()
		regionHillsFrac = CyFractal()
		regionPeaksFrac = CyFractal()
		if (rift_grain >= 0) :
			rFrac = CyFractal()
			rFrac.fracInit(iRegionWidth, iRegionHeight, rift_grain, self.dice, 0, iRegionFracXExp, iRegionFracYExp )
			if has_center_rift:
				iRegionPlotFlags += CyFractal.FracVals.FRAC_CENTER_RIFT
			regionContinentsFrac.fracInitRifts(iRegionWidth, iRegionHeight, iRegionGrain, self.dice, iRegionPlotFlags, 
			rFrac, iRegionFracXExp, iRegionFracYExp )
		else :   
			regionContinentsFrac.fracInit(iRegionWidth, iRegionHeight,
										  iRegionGrain, self.dice,
										  iRegionPlotFlags, iRegionFracXExp,
										  iRegionFracYExp)
		
		regionHillsFrac.fracInit(iRegionWidth, iRegionHeight,
								 iRegionHillsGrain, self.dice,
								 iRegionTerrainFlags, iRegionFracXExp,
								 iRegionFracYExp)
		regionPeaksFrac.fracInit(iRegionWidth, iRegionHeight,
								 iRegionHillsGrain+1, self.dice,
								 iRegionTerrainFlags, iRegionFracXExp,
								 iRegionFracYExp)

		iWaterThreshold = regionContinentsFrac.getHeightFromPercent(water)
		iHillsBottom1 = regionHillsFrac.getHeightFromPercent(max((25 - self.gc.getClimateInfo(self.map.getClimate()).getHillRange()), 0))
		iHillsTop1 = regionHillsFrac.getHeightFromPercent(min((25 + self.gc.getClimateInfo(self.map.getClimate()).getHillRange()), 100))
		iHillsBottom2 = regionHillsFrac.getHeightFromPercent(max((75 - self.gc.getClimateInfo(self.map.getClimate()).getHillRange()), 0))
		iHillsTop2 = regionHillsFrac.getHeightFromPercent(min((75 + self.gc.getClimateInfo(self.map.getClimate()).getHillRange()), 100))
		iPeakThreshold = regionPeaksFrac.getHeightFromPercent(self.gc.getClimateInfo(self.map.getClimate()).getPeakPercent())

		# Loop through the region's plots
		for x in range(iRegionWidth):
			for y in range(iRegionHeight):
				i = y*iRegionWidth + x
				val = regionContinentsFrac.getHeight(x,y)
				if val <= iWaterThreshold:
					self.plotTypes[i] = PlotTypes.PLOT_OCEAN
				else:
					self.plotTypes[i] = PlotTypes.PLOT_LAND
		
		if bShift :
			# Shift plots to obtain a more natural shape.
			self.shiftRegionPlots(iRegionWidth, iRegionHeight, iStrip)
					
		# Once the plot types for the region have been generated, they must be
		# applied to the global plot array.
		# Default approach is to ignore water and layer the lands over 
		for x in range(iRegionWidth):
			wholeworldX = x + iWestX
			for y in range(iRegionHeight):
				i = y*iRegionWidth + x
				if self.plotTypes[i] == PlotTypes.PLOT_OCEAN: continue
				wholeworldY = y + iSouthY
				iWorld = wholeworldY*self.iW + wholeworldX
				self.wholeworldPlotTypes[iWorld] = self.plotTypes[i]

		# This region is done.
		return

	#-------------------------------------------------------------------------
	# MultilayeredFractal class, controlling function.
	#
	def generatePlotsByRegion(self):
		# Sirian's MultilayeredFractal class, controlling function.
		# You -MUST- customize this function for each use of the class.
		global xShiftRoll
		iContinentsGrain = 1 + self.map.getCustomMapOption(0)
		iIslandsGrain = 4 + self.map.getCustomMapOption(1)
		userInputOverlap = self.map.getCustomMapOption(2)

		# Water variables need to differ if Overlap is set. Defining default here.
		iWater = 74

		# Add a few random patches of Tiny Islands first.
		numTinies = 1 + self.dice.get(4, "Tiny Islands - Custom Continents PYTHON")
		print("Patches of Tiny Islands: ", numTinies)
		if numTinies:
			for tiny_loop in range(numTinies):
				tinyWestLon = 0.01 * self.dice.get(85, "Tiny Longitude - Custom Continents PYTHON")
				tinyWestX = int(self.iW * tinyWestLon)
				tinySouthLat = 0.01 * self.dice.get(85, "Tiny Latitude - Custom Continents PYTHON")
				tinySouthY = int(self.iH * tinyWestLon)
				tinyWidth = int(self.iW * 0.15)
				tinyHeight = int(self.iH * 0.15)

				self.generatePlotsInRegion(80,
				                           tinyWidth, tinyHeight,
				                           tinyWestX, tinySouthY,
				                           4, 3,
				                           0, self.iTerrainFlags,
				                           6, 5,
				                           True, 3,
				                           -1, False,
				                           False
				                           )

		# North and South dimensions always fill the entire vertical span for this script.
		iSouthY = 0
		iNorthY = self.iH - 1
		iHeight = iNorthY - iSouthY + 1
		iWestX = 0
		iEastX = self.iW - 1
		iWidth = iEastX - iWestX + 1
		print("Cont South: ", iSouthY, "Cont North: ", iNorthY, "Cont Height: ", iHeight)

		# Add the Continents.
		# Horizontal dimensions may be affected by overlap and/or shift.
		if userInputOverlap: # Then both regions fill the entire map and overlap each other.
			# The west and east boundaries are already set (to max values).
			# Set X exponent to normal setting:
			xExp = 7
			# Also need to reduce amount of land plots, since there will be two layers in all areas.
			iWater = 82
		else: # The regions are separate, with continents only in one part, islands only in the other.
			# Set X exponent to square setting:
			xExp = 6
			# Handle horizontal shift for the Continents layer.
			# (This will choose one side or the other for this region then fit it properly in its space).
			if xShiftRoll:
				westShift = int(0.4 * self.iW)
				eastShift = 0
			else:
				westShift = 0
				eastShift = int(0.4 * self.iW)

			iWestX += westShift
			iEastX -= eastShift
			iWidth = iEastX - iWestX + 1
		print("Cont West: ", iWestX, "Cont East: ", iEastX, "Cont Width: ", iWidth)

		self.generatePlotsInRegion(iWater,
		                           iWidth, iHeight,
		                           iWestX, iSouthY,
		                           iContinentsGrain, 4,
		                           self.iRoundFlags, self.iTerrainFlags,
		                           xExp, 6,
		                           True, 15,
		                           -1, False,
		                           False
		                           )

		# Add the Islands.
		iWestX = 0
		iEastX = self.iW - 1
		iWidth = iEastX - iWestX + 1

		# Horizontal dimensions may be affected by overlap and/or shift.
		if userInputOverlap: # Then both regions fill the entire map and overlap each other.
			# The west and east boundaries are already set (to max values).
			# Set X exponent to normal setting:
			xExp = 7
			# Also need to reduce amount of land plots, since there will be two layers in all areas.
			iWater = 82
		else: # The regions are separate, with continents only in one part, islands only in the other.
			# Set X exponent to square setting:
			xExp = 6
			# Handle horizontal shift for the Continents layer.
			# (This will choose one side or the other for this region then fit it properly in its space).
			if xShiftRoll:
				westShift = 0
				eastShift = int(0.4 * self.iW)
			else:
				westShift = int(0.4 * self.iW)
				eastShift = 0

			iWestX += westShift
			iEastX -= eastShift
			iWidth = iEastX - iWestX + 1
		print("Island West: ", iWestX, "Island East: ", iEastX, "Isl Width: ", iWidth)


		self.generatePlotsInRegion(iWater,
		                           iWidth, iHeight,
		                           iWestX, iSouthY,
		                           iIslandsGrain, 5,
		                           self.iRoundFlags, self.iTerrainFlags,
		                           xExp, 6,
		                           True, 15,
		                           -1, False,
		                           False
		                           )

		# All regions have been processed. Plot Type generation completed.
		print "Done"
		return self.wholeworldPlotTypes

'''
Regional Variables Key:

iWaterPercent,
iRegionWidth, iRegionHeight,
iRegionWestX, iRegionSouthY,
iRegionGrain, iRegionHillsGrain,
iRegionPlotFlags, iRegionTerrainFlags,
iRegionFracXExp, iRegionFracYExp,
bShift, iStrip,
rift_grain, has_center_rift,
invert_heights
'''
#	
#END class PFHL_MultilayeredFractal
#

#-------------------------------------------------------------------------
# 
#
def generatePlotTypes():
	NiTextOut("Setting Plot Types (Python PlanetFall Highlands) ...")
	
	gc = CyGlobalContext()
	map = CyMap()
	dice = gc.getGame().getMapRand()
	iW = map.getGridWidth()
	iH = map.getGridHeight()
	plotTypes = [PlotTypes.PLOT_OCEAN] * (iW*iH)
	terrainFrac = CyFractal()
	
	fractal_world = PFHL_MultilayeredFractal()
	plotTypes = fractal_world.generatePlotsByRegion()
	terrainFrac.fracInit(iW, iH, h_grain, dice, 0, -1, -1)
	
	iHighlandThreshold = terrainFrac.getHeightFromPercent(h_highlands)
	iPeaksThreshold = iHighlandThreshold - (iHighlandThreshold * h_peaks) 

	# Now the main loop, which will assign the plot types.
	for x in range(iW):
		for y in range(iH):
			i = y*iW + x
			val = terrainFrac.getHeight(x,y)
			if plotTypes[i] == PlotTypes.PLOT_OCEAN:
				continue # Water plots already set.
			if val >= iHighlandThreshold:
				plotTypes[i] = PlotTypes.PLOT_HILLS
			elif val >= iPeaksThreshold and val < iHighlandThreshold:
				plotTypes[i] = PlotTypes.PLOT_PEAK
			else:
				pass

	return plotTypes

#-------------------------------------------------------------------------
# 
#
def generateTerrainTypes():
	NiTextOut("Generating Terrain (Python PlanetFall Highlands) ...")

	gc = CyGlobalContext()
	map = CyMap()
	iW = map.getGridWidth()
	iH = map.getGridHeight()
	dice = gc.getGame().getMapRand()

	iCoast = gc.getInfoTypeForString("TERRAIN_COAST")
	iShelf = gc.getInfoTypeForString("TERRAIN_SHELF")
	iOcean = gc.getInfoTypeForString("TERRAIN_OCEAN")
	iIce = gc.getInfoTypeForString("FEATURE_ICE")
	iTrench = gc.getInfoTypeForString("FEATURE_TRENCH")
	iTrenchImp = gc.getInfoTypeForString("IMPROVEMENT_TRENCH")
	prob_base = 1.00
	prob_shelf = 0
	prob_mod = 0.30
	prob_spawn = -0.30
	prob_trench = 0.45
	
	for x in range(iW):
		for y in range(iH):
			if ( y < 2 or y > (iH-2) or x == 0 or x == iW ) :
				continue
			pPlot = map.plot(x, y)
			if pPlot.getTerrainType() != iCoast:
				continue
				
			ocean_counter = 0
			trench_counter = 0
			highland_counter = 0
			prob_drop = 0.0
			
			pP1 = map.plot(x-1, y)
			if pP1.getTerrainType() == iOcean:
				ocean_counter += 1
			if pP1.getPlotType() == PlotTypes.PLOT_HILLS or pP1.getPlotType() == PlotTypes.PLOT_PEAK:
				highland_counter += 1
			if pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp:
				trench_counter += 1
			if pP1.getTerrainType() == iCoast:
				prob_drop = prob_drop * prob_mod + prob_mod
			pP1 = map.plot(x-1, y-1)
			if pP1.getTerrainType() == iOcean:
				ocean_counter += 1
			if pP1.getPlotType() == PlotTypes.PLOT_HILLS or pP1.getPlotType() == PlotTypes.PLOT_PEAK:
				highland_counter += 1
			if pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp:
				trench_counter += 1
			if pP1.getTerrainType() == iCoast:
				prob_drop = prob_drop * prob_mod + prob_mod
			pP1 = map.plot(x, y-1)
			if pP1.getTerrainType() == iOcean:
				ocean_counter += 1
			if pP1.getPlotType() == PlotTypes.PLOT_HILLS or pP1.getPlotType() == PlotTypes.PLOT_PEAK:
				highland_counter += 1
			if pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp:
				trench_counter += 1
			if pP1.getTerrainType() == iCoast:
				prob_drop = prob_drop * prob_mod + prob_mod
			pP1 = map.plot(x+1, y)
			if pP1.getTerrainType() == iOcean:
				ocean_counter += 1
			if pP1.getPlotType() == PlotTypes.PLOT_HILLS or pP1.getPlotType() == PlotTypes.PLOT_PEAK:
				highland_counter += 1
			if pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp:
				trench_counter += 1
			if pP1.getTerrainType() == iCoast:
				prob_drop = prob_drop * prob_mod + prob_mod
			pP1 = map.plot(x+1, y-1)
			if pP1.getTerrainType() == iOcean:
				ocean_counter += 1
			if pP1.getPlotType() == PlotTypes.PLOT_HILLS or pP1.getPlotType() == PlotTypes.PLOT_PEAK:
				highland_counter += 1
			if pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp:
				trench_counter += 1
			if pP1.getTerrainType() == iCoast:
				prob_drop = prob_drop * prob_mod + prob_mod
			pP1 = map.plot(x+1, y+1)
			if pP1.getTerrainType() == iOcean:
				ocean_counter += 1
			if pP1.getPlotType() == PlotTypes.PLOT_HILLS or pP1.getPlotType() == PlotTypes.PLOT_PEAK:
				highland_counter += 1
			if pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp:
				trench_counter += 1
			if pP1.getTerrainType() == iCoast:
				prob_drop = prob_drop * prob_mod + prob_mod
			pP1 = map.plot(x, y+1)
			if pP1.getTerrainType() == iOcean:
				ocean_counter += 1
			if pP1.getPlotType() == PlotTypes.PLOT_HILLS or pP1.getPlotType() == PlotTypes.PLOT_PEAK:
				highland_counter += 1
			if pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp:
				trench_counter += 1
			if pP1.getTerrainType() == iCoast:
				prob_drop = prob_drop * prob_mod + prob_mod
			pP1 = map.plot(x-1, y+1)
			if pP1.getTerrainType() == iOcean:
				ocean_counter += 1
			if pP1.getPlotType() == PlotTypes.PLOT_HILLS or pP1.getPlotType() == PlotTypes.PLOT_PEAK:
				highland_counter += 1
			if pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp:
				trench_counter += 1
			if pP1.getTerrainType() == iCoast:
				prob_drop = prob_drop * prob_mod + prob_mod
			
			prob = prob_base - prob_drop
			if( trench_counter == 0 ):
				prob = prob + prob_spawn
			if( trench_counter == 1 ):
				prob = prob + prob_trench
			if( trench_counter > 2 ):
				prob = 0
			if( ocean_counter == 0 ):
				prob = 0
			if( highland_counter == 0 ):
				prob = 0

			if prob * 100 > dice.get(100, "trench lines"):
				if pPlot.getFeatureType() == iIce:
					pPlot.setImprovementType(iTrenchImp)
				else :
					pPlot.setFeatureType(iTrench, 0 )

	for x in range(iW):
		for y in range(iH):
			if ( y < 2 or y > (iH-2) or x == 0 or x == iW ) :
				continue
			pPlot = map.plot(x, y)
			if pPlot.getTerrainType() != iOcean:
				continue
				
			prob_shelf = 0
			
			pP1 = map.plot(x-1, y)
			if pP1.getPlotType() != PlotTypes.PLOT_OCEAN:
				continue
			if pP1.getTerrainType() == iShelf or (pP1.getTerrainType() == iCoast and not (pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp)):
				prob_shelf = 50
			pP1 = map.plot(x-1, y-1)
			if pP1.getPlotType() != PlotTypes.PLOT_OCEAN:
				continue
			if pP1.getTerrainType() == iShelf or (pP1.getTerrainType() == iCoast and not (pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp)):
				prob_shelf = 50
			pP1 = map.plot(x, y-1)
			if pP1.getPlotType() != PlotTypes.PLOT_OCEAN:
				continue
			if pP1.getTerrainType() == iShelf or (pP1.getTerrainType() == iCoast and not (pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp)):
				prob_shelf = 50
			pP1 = map.plot(x+1, y-1)
			if pP1.getPlotType() != PlotTypes.PLOT_OCEAN:
				continue
			if pP1.getTerrainType() == iShelf or (pP1.getTerrainType() == iCoast and not (pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp)):
				prob_shelf = 50
			pP1 = map.plot(x+1, y)
			if pP1.getPlotType() != PlotTypes.PLOT_OCEAN:
				continue
			if pP1.getTerrainType() == iShelf or (pP1.getTerrainType() == iCoast and not (pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp)):
				prob_shelf = 50
			pP1 = map.plot(x+1, y+1)
			if pP1.getPlotType() != PlotTypes.PLOT_OCEAN:
				continue
			if pP1.getTerrainType() == iShelf or (pP1.getTerrainType() == iCoast and not (pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp)):
				prob_shelf = 50
			pP1 = map.plot(x, y+1)
			if pP1.getPlotType() != PlotTypes.PLOT_OCEAN:
				continue
			if pP1.getTerrainType() == iShelf or (pP1.getTerrainType() == iCoast and not (pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp)):
				prob_shelf = 50
			pP1 = map.plot(x-1, y+1)
			if pP1.getPlotType() != PlotTypes.PLOT_OCEAN:
				continue
			if pP1.getTerrainType() == iShelf or (pP1.getTerrainType() == iCoast and not (pP1.getFeatureType() == iTrench or pP1.getImprovementType() == iTrenchImp)):
				prob_shelf = 50

			if prob_shelf > dice.get(100, "Shelf extension"):
				pPlot.setTerrainType(iShelf, True, True )

	terraingen = TerrainGenerator()
	terrainTypes = terraingen.generateTerrain()
	return terrainTypes

#-------------------------------------------------------------------------
# 
#
def addFeatures():

	# Turning some Highlands into unpassable Terrain
	# This runs after the actual Terrain generation, so this
	# is only good to add impassable Terrain, over the terrain that was there
	# before.
	gc = CyGlobalContext()
	map = CyMap()
	iW = map.getGridWidth()
	iH = map.getGridHeight()
	dice = gc.getGame().getMapRand()

#	newTerrain = gc.getInfoTypeForString("TERRAIN_PEAKTERRAIN")
#	newPlot = PlotTypes.PLOT_PEAK

#	for x in range(iW):
#		for y in range(iH):
#			pPlot = map.plot(x, y)
#			if ( pPlot.getPlotType() != PlotTypes.PLOT_PEAK ) :
#				continue

#			pPlot.setPlotType(newPlot, True, True)
#			pPlot.setTerrainType(newTerrain, True, True )
				
	NiTextOut("Adding Features (Python PlanetFall Highlands) ...")
	featuregen = FeatureGenerator()
	featuregen.addFeatures()
	return 0

def normalizeAddRiver():
	return None

def normalizeRemovePeaks():
	return None

def normalizeAddLakes():
	return None

def normalizeRemoveBadFeatures():
	return None

def normalizeRemoveBadTerrain():
	return None

def normalizeAddFoodBonuses():
	return None

def normalizeAddGoodTerrain():
	return None
